<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */

function isloggedin() {
	global $db;
	if($_SESSION['user']) {
		$user = $db->q("tuple SELECT Login, Password FROM Users WHERE ID = %i", $_SESSION['user']);
		$hash = md5($user['Login'] . $user['Password'] . session_id());
		if($hash === $_SESSION['hash']) return true;
	}
	return false;
}
function checklogin() {
	if(!isloggedin()) { $_SESSION['afterLogin'] = getURL(); header("Location: login.php"); }
}
function print_header($pageTitle) {
	include "includes/header.html";
}
function print_footer() {
	include "includes/footer.html";
}

function getURL() {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return str_replace('fuji', 'home.terrabulgaria.com', $pageURL);
}
function focusOn($fieldName) {
	print "<script type='text/javascript'> document.getElementById('$fieldName').focus(); </script>	";
}

function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}
$takenAccessKeys = str_split("HBRGAL");
function assignAccessKey($name) {
	global $takenAccessKeys;
	$letters = str_split(preg_replace('/[^A-Za-z0-9]/', '', strtoupper(unhtmlentities($name))));
	for($i = 0; $i < count($letters); $i++)
	{
		if(in_array($letters[$i], array('.', ' ', '-', '_', "'", '"'))) continue;
		if(!in_array($letters[$i], $takenAccessKeys)) {
			array_push($takenAccessKeys, $letters[$i]);
			return $letters[$i];
		}
	}
	return false;
}
function indicateAccessKey($text, $accessKey) {
	return $accessKey ? preg_replace('/[' . strtoupper($accessKey) . strtolower($accessKey) . ']/', '<span class="accesskey">$0</span>', $text, 1) : $text;
}

session_start();
include_once("lib/lib.database.php");
include_once("lib/.database.password");
$db = new db("IOU", "localhost", "iou", databasePassword, true);
define(DEBUG, false);
define(moneyFormat, "&euro; %5#3.2i");
if(isloggedin()) 
	$gUser = $db->q("tuple SELECT * FROM Users WHERE ID = %i", $_SESSION['user']);
?>
